<template>
  <ElDialog
    v-model="dialogVisible"
    :title="`配置 ${dataSourceType} 数据源`"
    width="800"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      item
    >
      <el-form-item label="数据源名称：" prop="name">
        <el-input v-model="formData.name" placeholder="请输入数据源名称，最多50字符" />
      </el-form-item>
      <el-form-item label="主机：" prop="ip">
        <el-input v-model="formData.ip" placeholder="请输入服务器地址" />
      </el-form-item>
      <el-form-item label="端口：" prop="port">
        <el-input v-model="formData.port" placeholder="请输入端口" />
      </el-form-item>
      <el-form-item label="数据库名：" prop="dbname">
        <el-input v-model="formData.dbname" placeholder="请输入数据库" />
      </el-form-item>
      <el-form-item label="连接参数：" prop="param">
        <el-input v-model="formData.param" placeholder="请输入连接参数，如useUnicode=true&characterEncoding=utf8" />
      </el-form-item>
      <el-form-item label="用户名：" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input v-model="formData.password" placeholder="请输入密码" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <ElButton @click="dialogVisible = false">取消</ElButton>
        <ElButton :disabled="formLoading" type="primary" @click="submitForm">保存</ElButton>
      </span>
    </template>
  </ElDialog>
</template>

<script lang="ts" setup>
import {reactive, defineExpose, defineEmits,inject} from 'vue';
import * as PostApi from "@/api/etl/datasource";

defineProps({
  dataSourceType: String, // 定义 dataSourceType 属性
});


const formRef = ref() // 表单 Ref
const dialogVisible = ref(false) // 弹窗的是否展示

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const formLoading = ref(false) // 表单加载：1）修改时的数据加载；2）提交的按钮禁用

const formType = ref('') // 表单的类型：create - 新增；update - 修改

// 注入父组件提供的方法，当保存成功后，重新加载主页列表
const onSaveSuccess = inject('onSaveSuccess');


/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PostApi.getPost(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const formData = ref({
  name:'',
  type:'MySQL',
  dbname:'',
  ip:'',
  port:3306 ,
  param:'',
  username:'',
  password:'',
  zookeeperQuorum:'',
  zookeeperPort:'',
  hbaseRootDir:'',
  kerberosEnable:false,
  kerberosPrincipal:'',
  kerberosKeytabFilepath:'',
  ftpProtocol:'',
  txtPath:''
});

const formRules = reactive({
  name: [{ required: true, message: '数据源名称不能为空', trigger: 'blur' }],
  ip: [{ required: true, message: '服务器地址不能为空', trigger: 'blur' }],
  port: [{ required: true, message: '端口不能为空', trigger: 'blur' }],
  dbname: [{ required: true, message: '数据库名不能为空', trigger: 'blur' }],
  param: [{ required: false}],
  username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
})
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: '',
    type: 'MySQL',
    dbname: '',
    ip: '',
    port: 3306 ,
    param: '',
    username: '',
    password: '',
    zookeeperQuorum: '',
    zookeeperPort: '',
    hbaseRootDir: '',
    kerberosEnable: false,
    kerberosPrincipal: '',
    kerberosKeytabFilepath: '',
    ftpProtocol: '',
    txtPath: ''
  }
  // 然后调用resetFields来重置表单验证状态
  formRef.value?.resetFields();
}
/*提交表单*/
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PostApi.PostVO
    if (formType.value === 'create') {
      await PostApi.createPost(data)
      message.success(t('common.createSuccess'))
    } else {
      await PostApi.updatePost(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 保存成功，调用父窗口重新加载列表
    onSaveSuccess()

  } finally {
    formLoading.value = false
  }
}
const handleClose = () => {
  dialogVisible.value = false
};

</script>
<style scoped>

</style>

